<template>
  <div class="signup-page mx-auto p-3 w-330">
    <validate-form @form-submit="onFormSubmit">
      <div class="mb-3">
        <label for="exampleInputEmail1" class="form-label">邮箱地址</label>
        <validate-input
          placeholder="xxx@xxx.com"
          type="email"
          :rules="emailRules"
          v-model="formData.email"
        />
      </div>
      <div class="mb-3">
        <label for="exampleInputNickname1" class="form-label">昵称</label>
        <validate-input
          placeholder="你的昵称"
          type="text"
          :rules="nickNameRules"
          v-model="formData.nickName"
        />
      </div>
      <div class="mb-3">
        <label for="exampleInputPassword1" class="form-label">密码</label>
        <validate-input
          :rules="passwordRules"
          v-model="formData.password"
          placeholder="password"
          type="password"
        />
      </div>
      <div class="mb-3">
        <label for="exampleInputPassword1" class="form-label">重复密码</label>
        <validate-input
          placeholder="password"
          type="password"
          :rules="repeatPasswordRules"
          v-model="formData.repeatPassword"
        />
      </div>
      <template>
        <button type="submit" class="btn btn-primary btn-block btn-large">
          注册新用户
        </button>
      </template>
    </validate-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue'
import { useRouter } from 'vue-router'
import validateInput, { RulesProp } from '../components/ValidateInput.vue'
import ValidateForm from '../components/ValidateForm.vue'
import axios from 'axios'
import createMessage from '../components/createMessage'

export default defineComponent({
  name: 'Signup',
  components: {
    validateInput,
    ValidateForm
  },
  setup() {
    const router = useRouter()
    const formData = reactive({
      email: '',
      nickName: '',
      password: '',
      repeatPassword: ''
    })
    const emailRules: RulesProp = [
      { type: 'require', message: '电子邮箱地址不能为空' },
      { type: 'email', message: '请输入正确的电子邮箱格式' },
      { type: 'range', message: '输入的字符必须大于3并且小于25' }
    ]
    const passwordRules: RulesProp = [
      { type: 'require', message: '密码不能为空' }
    ]
    const nickNameRules: RulesProp = [
      { type: 'require', message: '昵称不能为空' }
    ]
    const repeatPasswordRules: RulesProp = [
      { type: 'require', message: '重复密码不能为空' },
      {
        type: 'same',
        validator: () => {
          return formData.password === formData.repeatPassword
        },
        message: '两次密码不一致哦'
      }
    ]
    const onFormSubmit = async(result: boolean) => {
      if (result) {
        // 返回登录页并实现signup注册
        const payload = {
          email: formData.email,
          password: formData.password,
          nickName: formData.nickName
        }
        // 没有修改 store 所以不用修改 store 增加 mutation
        axios.post('/users/', payload).then(() => {
          createMessage('注册成功，正在跳转登录页面', 'success')
          setTimeout(() => {
            router.push('/login')
          }, 2000)
        }).catch(e => {
          console.log(e)
        })
      }
    }
    return {
      emailRules,
      passwordRules,
      nickNameRules,
      repeatPasswordRules,
      formData,
      onFormSubmit
    }
  }
})
</script>

<style>
</style>
